package cn.enjoy.tag;

import cn.enjoy.tag.entity.User;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 * @author K
 * @date 2024-02-27 9:28
 */
public class UserBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser {

    /**
     *  Element对应的类
     */
    @Override
    protected Class getBeanClass (Element element) {
        return User.class;
    }

    /**
     *  功能描述：从element中解析并提取对应的元素
     */
    @Override
    public void doParse (Element element, BeanDefinitionBuilder bean) {
        String userName = element.getAttribute("userName");
        String email    = element.getAttribute("email");

        // 将提取的数据放入到 BeanDefinitionBuilder中，待到完成所有 bean 的解析后统一注册到beanFactory 中
        if(StringUtils.hasText(userName)) {
            bean.addPropertyValue("userName", userName);
        }
        if(StringUtils.hasText(email)) {
            bean.addPropertyValue("email", email);
        }
    }
}
